﻿﻿<form id="frmBXJGCMSColumn" class="frm-container">
    <input type="hidden" name="id" value="0" />
    <table class="frm-box">
        <tr>
            <th>上级栏目</th>
            <td><input name="parentId" value="0" /></td>
            <th>系统预定义</th>
            <td><input name="isSysDefine" class="easyui-checkbox" value="true" /></td>
        </tr>
        <tr>
            <th>栏目名称</th>
            <td colspan="3"><input name="displayName" class="easyui-textbox" data-options="required:true,validType:'length[0,50]',width:'115%'" /></td>
        </tr>
        <tr>
            <th>栏目类型</th>
            <td>
                <input name="columnType" />
            </td>
            <th>内容类型</th>
            <td>
                <input name="contentTypeId"  />
            </td>
        </tr>
        <tr>
            <th>列表页模板</th>
            <td><input name="listTemplate" class="easyui-textbox" data-options="validType:'length[0,50]'" /></td>
            <th>内容页模板</th>
            <td><input name="detailTemplate" class="easyui-textbox" data-options="validType:'length[0,50]'" /></td>
        </tr>
        <tr>
            <th>SEO标题</th>
            <td colspan="3"><input name="seoTitle" class="easyui-textbox" data-options="validType:'length[0,50]'" /></td>
        </tr>
        <tr>
            <th>SEO关键字</th>
            <td colspan="3"><input name="seoKeyword" class="easyui-textbox" data-options="validType:'length[0,50]'" /></td>
        </tr>
        <tr>
            <th>SEO描述</th>
            <td colspan="3"><input name="seoDescription" class="easyui-textbox" data-options="validType:'length[0,50]'" /></td>
        </tr>
        <tr>
            <th>描述</th>
            <td colspan="3"><input name="description" class="easyui-textbox" data-options="validType:'length[0,50]'" /></td>
        </tr>
    </table>
</form>
<script>
    (function () {
        const m = 'BXJGCMSColumn';                                      //模块标识
        const $dlg = $('#dlg' + m);                                     //弹窗jq对象
        const $frm = $('form');                                         //表单jq对象
        const $cbParent = $frm.find("[name='parentId']");               //父节点下拉框jq对象
        const $cbColumnType = $frm.find("[name='columnType']");         //栏目类型下拉框jq对象
        const $cbContentType = $frm.find("[name='contentTypeId']");     //内容类型下拉框jq对象
        const $name =  $frm.find("[name='displayName']");               //标题名称输入框jq对象

        let initParentTask = $.Deferred();                              //初始化父节点的任务
        let initFormTask = $.Deferred();                                //修改状态初始化加载要修改的数据的任务
        let initColumnTypeTask = $.Deferred();                          //初始化栏目类型下拉框加载数据的任务
        let initContentTypeTask = $.Deferred();                         //加载内容类型下拉框数据的任务
        
        let parent = $dlg.data('parent');                               //新增时，列表页传递过来的父节点的id
        let id = $dlg.data('id');                                       //修改时，列表页传递过来的要修改的对象的id

        //初始化父节点
        $cbParent.combotree({
            url: abp.appPath + 'api/services/app/' + m + '/GetTreeForSelect',
            queryParams: { forType: 4, parentText: '全部' },
            prompt: abp.localization.bxjgUtils('全部'),
            onLoadSuccess: function (node, data) {
                initParentTask.resolve();
            },
            loadFilter: function (data, parent) {
                data = $.fn.combotree.defaults.loadFilter(data, parent);
                if (data && data[0])
                    data[0].id = 0;//奇怪这里用'0' 与输入口默认值对应不起来
                return data;
            }
        });
        //初始化栏目类型
        $cbColumnType.combobox({
            editable:false,
            required: true,
            url: abp.appPath+'api/services/app/bxjgcmscommon/getcolumntypes',
            queryParams: { forType: 4 },
            onLoadSuccess: function (node, data) {
                initColumnTypeTask.resolve();
            }
        });
        //初始化内容类型
        $cbContentType.combobox({
            editable:false,
            required: true,
            url:abp.appPath+'api/services/app/GeneralTree/GetNodesForSelect',
            queryParams: { forType: 4,parentId:21 },
            onLoadSuccess: function (node, data) {
                initContentTypeTask.resolve();
            }
        });

        //没必要等下拉加载完再赋值，因为easyui会在加载完后自动处理已存在的值，其它控件也一样
        if (id) {
            //修改时初始化加载表单并绑定到控件
            initFormTask = abp.services.app[abp.utils.toCamelCase(m)].get({ id: id }).done(function (d) {
                $frm.form('load', d);
            });
        } else {
            //新增时，初始化父节点的值
            $cbParent.combotree('setValue', parent);
            initFormTask.resolve();
        }

        //等待所有数据初始化完成，显示loading遮罩
        abp.ui.setBusy($dlg, $.when(initFormTask, initParentTask, initColumnTypeTask, initContentTypeTask));

        //聚焦到名称输入框
        setTimeout(function () {
            $name.next('span').find('input').focus();
        }, 0);

        //点击保存时的处理逻辑
        $dlg.data('clickSave', function () {
            if (!$frm.form('validate'))
                return;
            var postData = $frm.serializeJSON(/*{ checkboxUncheckedValue: "false" }*/);
            var defer;
            if (id) {
                defer = abp.services.app.bXJGCMSColumn.update(postData).done(function (d) {
                    abp.notify.success(abp.localization.bxjgUtils('修改成功！'));
                });
            } else {
                defer = abp.services.app.bXJGCMSColumn.create(postData).done(function (d) {
                    abp.notify.success(abp.localization.bxjgUtils('新增成功！'));
                });
            }
            defer.done(function () {
                onSaved();
            });
            abp.ui.setBusy($dlg, defer);
        });

        //保存后回调通知列表页
        function onSaved() {
            var f = $dlg.data('onSaved');
            if (f)
                f();
        }
    })();
</script>
